package com.itheima.reggie.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    /**
     * 用户下订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> order(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 用户端展示自己的订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        log.info("用户端展示自己的订单分页查询");
        //在service层自定义的page()方法
        Page<OrderDto> pageInfo = orderService.page(page, pageSize);
        return R.success(pageInfo);
    }

    /**
     * 订单分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime) {
        log.info("page:{},pageSize:{}", page, pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件查询构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //模糊查询订单号
        if (number != null) {
            queryWrapper.like(Orders::getNumber, number);
        }
        if (beginTime != null) {
            //查询beginTime 下单时间大于等于这个时间
            queryWrapper.gt(Orders::getOrderTime, beginTime);
        }
        if (endTime != null) {
            //查询endTime 下单时间小与等于这个时间
            queryWrapper.lt(Orders::getOrderTime, endTime);
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //执行查询,调用分页查询
        orderService.page(pageInfo, queryWrapper);
        //将userName赋上值---consignee,就是用户的姓名,因为userName在用户登录的时候并没有赋值
        List<Orders> records = pageInfo.getRecords();
            records.stream().map((item)->{
                String consignee = item.getConsignee();
                item.setUserName(consignee);
                return item;
            }).collect(Collectors.toList());
            pageInfo.setRecords(records);
        return R.success(pageInfo);
    }

    /**
     * 修改订单的状态
     * //订单状态 1待付款，2待派送，3已派送，4已完成，5已取消
     * private Integer status;
     *
     * @param map
     * @return
     */
    @PutMapping
    public R<String> changeOrderStatus(@RequestBody Map map) {
        log.info("map:{}", map);
        orderService.changeOrderStatus(map);
        return R.success("订单状态修改成功");
    }

    /**
     * 当订单的status==4时（该订单已经完成），会有再来一单的操作
     * @param map
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Map<String,String> map){
        orderService.again(map);
        return R.success("操作成功");
    }

}
